<!DOCTYPE html>
<html lang=zh-CN>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta property="og:description" content="samuel-24276的博客">
    <meta property="og:type" content="website">
    <meta name="description" content="samuel-24276的博客">
    <meta name="keyword"  content="aircloud">
    <link rel="shortcut icon" href="/myblog/img/favicon.ico">

    <title>
        
        浅谈xpath及其使用 - samuel-24276的博客 | samuel-24276&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/myblog/css/aircloud.css">

    
<link rel="stylesheet" href="/myblog/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_28hi1hpxx24.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 7.1.1"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> Like what you do, do what you like </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/myblog/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>samuel-24276</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/myblog/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/myblog/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/myblog/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/myblog/collect/">
                    <i class="iconfont icon-shoucang1"></i>
                    <span>收藏</span>
                </a>
            </li>
            <li >
                <a href="/myblog/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-xpath%E8%8A%82%E7%82%B9"><span class="toc-text">1.xpath节点</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E8%8A%82%E7%82%B9%EF%BC%88Node%EF%BC%89"><span class="toc-text">1.1.节点（Node）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E8%8A%82%E7%82%B9%E5%85%B3%E7%B3%BB"><span class="toc-text">1.2.节点关系</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-1-%E7%88%B6%EF%BC%88Parent%EF%BC%89"><span class="toc-text">1.2.1.父（Parent）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-2-%E5%AD%90%EF%BC%88Children%EF%BC%89"><span class="toc-text">1.2.2.子（Children）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-3-%E5%90%8C%E8%83%9E%EF%BC%88Sibling%EF%BC%89"><span class="toc-text">1.2.3.同胞（Sibling）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-4-%E5%85%88%E8%BE%88%EF%BC%88Ancestor%EF%BC%89"><span class="toc-text">1.2.4.先辈（Ancestor）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-5-%E5%90%8E%E4%BB%A3%EF%BC%88Descendant%EF%BC%89"><span class="toc-text">1.2.5.后代（Descendant）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E5%9F%BA%E6%9C%AC%E5%80%BC%EF%BC%88Atomic-value%EF%BC%89"><span class="toc-text">1.3.基本值（Atomic value）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-%E9%A1%B9%E7%9B%AE%EF%BC%88Item%EF%BC%89"><span class="toc-text">1.4.项目（Item）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-xpath%E8%AF%AD%E6%B3%95"><span class="toc-text">2.xpath语法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-XML-%E5%AE%9E%E4%BE%8B%E6%96%87%E6%A1%A3"><span class="toc-text">2.1.XML 实例文档</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E9%80%89%E5%8F%96%E8%8A%82%E7%82%B9"><span class="toc-text">2.2.选取节点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%AE%9E%E4%BE%8B"><span class="toc-text">2.3.实例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E8%B0%93%E8%AF%AD%EF%BC%88Predicates%EF%BC%89"><span class="toc-text">2.3.谓语（Predicates）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E9%80%89%E5%8F%96%E8%8B%A5%E5%B9%B2%E8%B7%AF%E5%BE%84"><span class="toc-text">2.4.选取若干路径</span></a></li></ol></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-bg" id="search-bg"></div>
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> Like what you do, do what you like </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        浅谈xpath及其使用
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2020-03-29 22:14:38</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/myblog/tags/#工具" title="工具">工具</a>
        <span>/</span>
        
        <a class="tag" href="/myblog/tags/#xml" title="xml">xml</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <blockquote>
<p>XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。</p>
</blockquote>
<h1 id="1-xpath节点"><a href="#1-xpath节点" class="headerlink" title="1.xpath节点"></a>1.xpath节点</h1><blockquote>
<p>在 XPath 中，有七种类型的节点：元素、属性、文本、命名空间、处理指令、注释以及文档节点（或称为根节点）。</p>
</blockquote>
<h2 id="1-1-节点（Node）"><a href="#1-1-节点（Node）" class="headerlink" title="1.1.节点（Node）"></a>1.1.节点（Node）</h2><p>请看下面这个 XML 文档：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;ISO-8859-1&quot;</span>?&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">bookstore</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">book</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span>Harry Potter<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">author</span>&gt;</span>J K. Rowling<span class="tag">&lt;/<span class="name">author</span>&gt;</span> </span><br><span class="line">  <span class="tag">&lt;<span class="name">year</span>&gt;</span>2005<span class="tag">&lt;/<span class="name">year</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">price</span>&gt;</span>29.99<span class="tag">&lt;/<span class="name">price</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">book</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">bookstore</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>上面的XML文档中的节点例子：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bookstore</span>&gt;</span><span class="tag">&lt;<span class="name">book</span>&gt;</span> （文档节点即一级结点）</span><br><span class="line"><span class="tag">&lt;<span class="name">author</span>&gt;</span>J K. Rowling<span class="tag">&lt;/<span class="name">author</span>&gt;</span> （元素节点）</span><br><span class="line"><span class="tag">&lt;<span class="name">year</span>&gt;</span>2005<span class="tag">&lt;/<span class="name">year</span>&gt;</span> （元素节点）</span><br><span class="line"><span class="tag">&lt;<span class="name">price</span>&gt;</span>29.99<span class="tag">&lt;/<span class="name">price</span>&gt;</span> （元素节点）</span><br><span class="line">lang=&quot;en&quot; （属性节点）</span><br></pre></td></tr></table></figure>

<h2 id="1-2-节点关系"><a href="#1-2-节点关系" class="headerlink" title="1.2.节点关系"></a>1.2.节点关系</h2><h3 id="1-2-1-父（Parent）"><a href="#1-2-1-父（Parent）" class="headerlink" title="1.2.1.父（Parent）"></a>1.2.1.父（Parent）</h3><p>每个元素以及属性都有一个父。</p>
<p>在上面的例子中，book 元素是 title、author、year 以及 price 元素的父。</p>
<h3 id="1-2-2-子（Children）"><a href="#1-2-2-子（Children）" class="headerlink" title="1.2.2.子（Children）"></a>1.2.2.子（Children）</h3><p>元素节点可有零个、一个或多个子。</p>
<p>在上面的例子中，title、author、year 以及 price 元素都是 book 元素的子。</p>
<h3 id="1-2-3-同胞（Sibling）"><a href="#1-2-3-同胞（Sibling）" class="headerlink" title="1.2.3.同胞（Sibling）"></a>1.2.3.同胞（Sibling）</h3><p>拥有相同的父的节点</p>
<p>在上面的例子中，title、author、year 以及 price 元素都是同胞。同胞之间可以使用下标访问。</p>
<h3 id="1-2-4-先辈（Ancestor）"><a href="#1-2-4-先辈（Ancestor）" class="headerlink" title="1.2.4.先辈（Ancestor）"></a>1.2.4.先辈（Ancestor）</h3><p>某节点的父、父的父，等等。</p>
<p>在上面的例子中，title 元素的先辈是 book 元素和 bookstore 元素。</p>
<h3 id="1-2-5-后代（Descendant）"><a href="#1-2-5-后代（Descendant）" class="headerlink" title="1.2.5.后代（Descendant）"></a>1.2.5.后代（Descendant）</h3><p>某个节点的子，子的子，等等。</p>
<p>在上面的例子中，bookstore 的后代是 book、title、author、year 以及 price 元素：</p>
<h2 id="1-3-基本值（Atomic-value）"><a href="#1-3-基本值（Atomic-value）" class="headerlink" title="1.3.基本值（Atomic value）"></a>1.3.基本值（Atomic value）</h2><p>基本值是无父或无子的节点。</p>
<p>基本值的例子：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">J</span> <span class="built_in">K</span><span class="operator">.</span> <span class="variable">Rowling</span></span><br><span class="line"><span class="string">&quot;en&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="1-4-项目（Item）"><a href="#1-4-项目（Item）" class="headerlink" title="1.4.项目（Item）"></a>1.4.项目（Item）</h2><p>项目是基本值或者节点。</p>
<h1 id="2-xpath语法"><a href="#2-xpath语法" class="headerlink" title="2.xpath语法"></a>2.xpath语法</h1><blockquote>
<p>XPath 使用路径表达式来选取 XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的。</p>
</blockquote>
<h2 id="2-1-XML-实例文档"><a href="#2-1-XML-实例文档" class="headerlink" title="2.1.XML 实例文档"></a>2.1.XML 实例文档</h2><p>我们将在下面的例子中使用这个 XML 文档。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;ISO-8859-1&quot;</span>?&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">bookstore</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">book</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span> <span class="attr">lang</span>=<span class="string">&quot;eng&quot;</span>&gt;</span>Harry Potter<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">price</span>&gt;</span>29.99<span class="tag">&lt;/<span class="name">price</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">book</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">book</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span> <span class="attr">lang</span>=<span class="string">&quot;eng&quot;</span>&gt;</span>Learning XML<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">price</span>&gt;</span>39.95<span class="tag">&lt;/<span class="name">price</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">book</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">bookstore</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="2-2-选取节点"><a href="#2-2-选取节点" class="headerlink" title="2.2.选取节点"></a>2.2.选取节点</h2><p>XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。</p>
<p>下面列出了最有用的路径表达式：</p>
<table>
<thead>
<tr>
<th>表达式</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>nodename</td>
<td>选取此节点的所有子节点</td>
</tr>
<tr>
<td>&#x2F;</td>
<td>从根节点选取</td>
</tr>
<tr>
<td>&#x2F;&#x2F;</td>
<td>从匹配选择的当前节点选择文档中的节点，而不考虑它们的位置</td>
</tr>
<tr>
<td>.</td>
<td>选取当前节点</td>
</tr>
<tr>
<td>..</td>
<td>选取当前节点的父节点</td>
</tr>
<tr>
<td>@</td>
<td>选取属性</td>
</tr>
<tr>
<td>text()</td>
<td>选取标签内的文字</td>
</tr>
</tbody></table>
<h2 id="2-3-实例"><a href="#2-3-实例" class="headerlink" title="2.3.实例"></a>2.3.实例</h2><p>在下面的表格中，我们已列出了一些路径表达式以及表达式的结果</p>
<table>
<thead>
<tr>
<th>路径表达式</th>
<th>结果</th>
</tr>
</thead>
<tbody><tr>
<td>bookstore</td>
<td>选取 bookstore 元素的所有子节点。</td>
</tr>
<tr>
<td>&#x2F;bookstore</td>
<td>选取根元素 bookstore。注释：假如路径起始于正斜杠( &#x2F; )，则此路径始终代表到某元素的绝对路径！</td>
</tr>
<tr>
<td>bookstore&#x2F;book</td>
<td>选取属于 bookstore 的子元素的所有 book 元素。</td>
</tr>
<tr>
<td>&#x2F;&#x2F;book</td>
<td>选取所有 book 子元素，而不管它们在文档中的位置。</td>
</tr>
<tr>
<td>bookstore&#x2F;&#x2F;book</td>
<td>选择属于 bookstore 元素的后代的所有 book 元素，而不管它们位于 bookstore 之下的什么位置。</td>
</tr>
<tr>
<td>&#x2F;&#x2F;@lang</td>
<td>选取名为 lang 的所有属性。</td>
</tr>
</tbody></table>
<h2 id="2-3-谓语（Predicates）"><a href="#2-3-谓语（Predicates）" class="headerlink" title="2.3.谓语（Predicates）"></a>2.3.谓语（Predicates）</h2><p>谓语用来查找某个特定的节点或者包含某个指定的值的节点。</p>
<p>谓语被嵌在方括号[]中。</p>
<h2 id="2-4-选取若干路径"><a href="#2-4-选取若干路径" class="headerlink" title="2.4.选取若干路径"></a>2.4.选取若干路径</h2><p>通过在路径表达式中使用“|”运算符，您可以选取若干个路径。</p>
<p>实例<br>在下面的表格中，我们列出了一些路径表达式，以及这些表达式的结果：</p>
<table>
<thead>
<tr>
<th>路径表达式</th>
<th>结果</th>
</tr>
</thead>
<tbody><tr>
<td>&#x2F;&#x2F;book&#x2F;title &#124; &#x2F;&#x2F;book&#x2F;price</td>
<td>选取 book 元素的所有 title 和 price 元素。</td>
</tr>
<tr>
<td>&#x2F;&#x2F;title &#124; &#x2F;&#x2F;price</td>
<td>选取文档中的所有 title 和 price 元素。</td>
</tr>
<tr>
<td>&#x2F;bookstore&#124;book&#x2F;title  &#x2F;&#x2F;price</td>
<td>选取属于 bookstore 元素的 book 元素的所有 title 元素，以及文档中所有的 price 元素。</td>
</tr>
</tbody></table>
<blockquote>
<p>以上内容来自<a target="_blank" rel="noopener" href="https://www.w3school.com.cn/xpath/index.asp">w3cschool的xpath教程</a>，为学习方便，将其内容迁移至自己的博客。</p>
</blockquote>
<hr>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/myblog/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>
        <div id="lv-container"></div>
        <div class="giscus"></div>
    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/samuel-24276">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://github.com/samuel-24276">samuel-24276&#39;s Page</a></span>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/">北京-未备案</a></span>
        <span>/</span>
        
        <span><a href="#">返回顶部</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/myblog/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/myblog/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






    <script type="text/javascript">
       (function() {
           if (typeof LivereTower === 'function') { return; }

           var j, d = document.getElementById('lv-container');

           d.setAttribute('data-id','city');
           d.setAttribute('data-uid' , 'MTAyMC80OTA5OC8yNTU5Mw==');

           j = document.createElement('script');
           j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
           j.async = true;

           d.appendChild(j);
       })();
    </script>
    <noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
    </div>

</html>
